package cn.fastoo.common.dao.system;


import cn.fastoo.common.entity.member.Member;
import cn.fastoo.common.entity.system.AdminSystemConfig;
import cn.fastoo.common.entity.system.AdminSystemMsg;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.*;
import java.util.concurrent.*;


@Component
public class AdminSystemMsgDao extends FastDaoImpl<AdminSystemMsg> implements FastDao<AdminSystemMsg> {

    public Logger logger = LoggerFactory.getLogger(AdminSystemMsg.class);

    @Autowired
    @Qualifier("mainDS")
    private void setJt(DataSource ds){
        this.jt = new JdbcTemplate(ds);
    }

    public static ExecutorService executorService = new ThreadPoolExecutor(10,200,5000, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>(Integer.MAX_VALUE),new RejectedExecutionHandler(){

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {

        }
    });

    public void yujing(List<Member> members,Integer shifangDay){
        List<AdminSystemMsg> newMsgs = new ArrayList<>();
        members.forEach(t->{
            long betweenDay = DateUtil.between(t.getCollectTime(), new Date(), DateUnit.DAY);
            AdminSystemMsg newMsg = new AdminSystemMsg();
            newMsg.setAdminUserId(t.getSalesUserId());
            newMsg.setContent(String.format("客户[%s]已经[%s]天没有成为正式会员了. 释放日期为[%s]",
                    t.getUsername(),
                    betweenDay,
                    DateUtil.formatDateTime( DateUtil.offsetDay(t.getCollectTime(), shifangDay) )));
            newMsg.setDirect(AdminSystemMsg.DIRECT_预警);
            newMsg.setDirectId(t.getId().toString());
            newMsgs.add(newMsg);
        });
        executorService.submit(() -> {
            try {
                batchInsert(newMsgs);
            }catch (Exception e){
                e.printStackTrace();
            }

        });
    }
    public void shifang(List<Member> members){
        List<AdminSystemMsg> newMsgs = new ArrayList<>();
        members.forEach(t->{
            long betweenDay = DateUtil.between(t.getCollectTime(), new Date(), DateUnit.DAY);
            AdminSystemMsg newMsg = new AdminSystemMsg();
            newMsg.setAdminUserId(t.getSalesUserId());
            newMsg.setContent(String.format("客户[%s]已经[%s]天没有成为正式会员了. 被系统释放了",
                    t.getUsername(),
                    betweenDay));
            newMsg.setDirect(AdminSystemMsg.DIRECT_预警);
            newMsg.setDirectId(t.getId().toString());
            newMsgs.add(newMsg);
        });
        executorService.submit(() -> {
            try {
                batchInsert(newMsgs);
            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }

}
